﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

using ERPStore.Web.Html;
using ERPStore.Web.Routing;
using ERPStore.Extensions;

using Microsoft.Practices.Unity;

namespace ERPStore.Localization.Html
{
	public static class LocalizedBreadCrumbExtensions
	{
		/// <summary>
		/// Retourne la liste des liens sous la forme Clé/Url pour former un 
		/// chemin de fer lorsque le model en cours est une liste de categorie de produits
		/// </summary>
		/// <param name="helper">The helper.</param>
		/// <param name="list">The list.</param>
		/// <returns></returns>
		public static IList<ERPStore.Models.BreadCrumbItem> GetLocalizedBreadcrumb(this UrlHelper helper, IList<ERPStore.Models.ProductCategory> list)
		{
			var language = helper.GetCurrentLanguage();
			var result = CreateDefaultLocalizedBreadcrumb(helper);
			var localizationService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Services.ILocalizationService>();
			var categoriesContent = localizationService.GetLocalizedContent("App_GlobalResources", language, null, "Categories", "Categories", ERPStore.Models.LocalizedContentType.Label);

			result.Add(new ERPStore.Models.BreadCrumbItem()
			{
				Title = categoriesContent, 
				Url = helper.RouteUrl(string.Format("ProductCategories-{0}", language))
			});
			return result;
		}

		/// <summary>
		/// Retourne la liste des liens sous la forme Clé/Url pour former un 
		/// chemin de fer lorsque le model en cours est une categorie de produits
		/// </summary>
		/// <param name="helper">The helper.</param>
		/// <param name="list">The list.</param>
		/// <returns></returns>
		public static IList<ERPStore.Models.BreadCrumbItem> GetLocalizedBreadcrumb(this UrlHelper helper, ERPStore.Models.ProductCategory category)
		{
			var result = helper.GetLocalizedBreadcrumb(new List<ERPStore.Models.ProductCategory>());
			if (category == null)
			{
				return result;
			}

			// On depile le path
			var path = new Dictionary<string, string>();
			path.Add(category.Localize(() => category.Name, helper.GetCurrentLanguage()) , helper.LocalizedHref(category));
			var c = category.Parent;
			while (true)
			{
				if (c != null)
				{
					path.Add(c.Localize(() => c.Name , helper.GetCurrentLanguage()), helper.LocalizedHref(c));
				}
				else
				{
					break;
				}
				c = c.Parent;
			}

			// Ajout inverse du plus grand au plus petit
			var reverse = path.Keys.Reverse();
			foreach (var item in reverse)
			{
				result.Add(new ERPStore.Models.BreadCrumbItem()
				{
					Title = item, 
					 Url = path[item]
				});
			}

			return result;
		}

		/// <summary>
		/// Retourne la liste des liens sous la forme Clé/Url pour former un 
		/// chemin de fer lorsque le model en cours est un produit
		/// </summary>
		/// <param name="helper">The helper.</param>
		/// <param name="list">The list.</param>
		/// <returns></returns>
		public static IList<ERPStore.Models.BreadCrumbItem> GetLocalizedBreadcrumb(this UrlHelper helper, ERPStore.Models.Product product)
		{
			var result = CreateDefaultLocalizedBreadcrumb(helper);
			if (product.Category != null)
			{
				var list = helper.GetLocalizedBreadcrumb(product.Category);
				foreach (var item in list)
				{
					if (!result.Any(i => i.Title == item.Title))
					{
						result.Add(new ERPStore.Models.BreadCrumbItem()
						{
							Title = item.Title, 
							Url = item.Url
						});
					}
				}
			}

			if (!result.Any(i => i.Title == product.Title))
			{
				result.Add(new ERPStore.Models.BreadCrumbItem()
				{
					Title = product.Localize(() => product.Title, helper.GetCurrentLanguage()), 
					Url = helper.LocalizedHref(product)
				});
			}

			return result;
		}

		/// <summary>
		/// Retourne la liste des liens sous la forme Clé/Url pour former un 
		/// chemin de fer lorsque le model en cours est une liste de marque
		/// </summary>
		/// <param name="helper">The helper.</param>
		/// <param name="list">The list.</param>
		/// <returns></returns>
		public static IList<ERPStore.Models.BreadCrumbItem> GetLocalizedBreadcrumb(this UrlHelper helper, IList<ERPStore.Models.Brand> list)
		{
			var result = CreateDefaultLocalizedBreadcrumb(helper);
			return result;
		}

		/// <summary>
		/// Retourne la liste des liens sous la forme Clé/Url pour former un 
		/// chemin de fer lorsque le model en cours est une marque
		/// </summary>
		/// <param name="helper">The helper.</param>
		/// <param name="list">The list.</param>
		/// <returns></returns>
		public static IList<ERPStore.Models.BreadCrumbItem> GetLocalizedBreadcrumb(this UrlHelper helper, ERPStore.Models.Brand brand)
		{
			var result = helper.GetBreadcrumb(new List<ERPStore.Models.Brand>());
			var item = new ERPStore.Models.BreadCrumbItem()
			{
				Title = brand.Name,
				Url = helper.Href(brand),
				Type = "Brand",
			};
			result.Add(item);
			return result;
		}

		/// <summary>
		/// Retourne la liste des liens sous la forme Clé/Url pour former un 
		/// chemin de fer lorsque le model en cours est une recherche de produits
		/// </summary>
		/// <param name="helper">The helper.</param>
		/// <param name="search">The search.</param>
		/// <returns></returns>
		public static IList<ERPStore.Models.BreadCrumbItem> GetLocalizedBreadcrumb(this UrlHelper helper, ERPStore.Models.ProductList search)
		{
			var result = CreateDefaultLocalizedBreadcrumb(helper);
			result.Add(new ERPStore.Models.BreadCrumbItem()
			{
				Title = helper.Encode(search.Query),
				Url = helper.RouteERPStoreUrl(ERPStore.Web.Routing.ERPStoreRoutes.PRODUCT_SEARCH, new { s = helper.Encode(search.Query) })
			});
			return result;
		}

		private static IList<ERPStore.Models.BreadCrumbItem> CreateDefaultLocalizedBreadcrumb(UrlHelper helper)
		{
			var language = helper.GetCurrentLanguage();
			var localizationService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Services.ILocalizationService>();
			var homeContent = localizationService.GetLocalizedContent("App_GlobalResources", language, null, "Home", "Accueil", ERPStore.Models.LocalizedContentType.Label);
			var result = new List<ERPStore.Models.BreadCrumbItem>();
			result.Add( new ERPStore.Models.BreadCrumbItem()
			{
				Title = homeContent, 
				Url =  helper.RouteERPStoreUrl(ERPStore.Web.Routing.ERPStoreRoutes.HOME, null)
			});
			return result;
		}

	}
}
